home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcr / pcr4_4.lha / DIST / threads / MkPCRSwap.c < prev    next >
C/C++ Source or Header  |  1990-12-10  |  5KB  |  195 lines

  1. /*
  2.  * MkPCRSwap.c
  3.  *
  4.  * Demers, December 10, 1990 9:03:12 am PST
  5.  *
  6.  * Make PCR swap directories
  7.  */
  8.  
  9. #include "xr/BasicTypes.h"
  10. #include "xr/Errno.h"
  11. #include "xr/ThreadsSharedMemBSDUtils.h"
  12.  
  13. #include <stdio.h>
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <unistd.h>
  17.  
  18. #define FAIL_IF_NOT_ISVTX    0
  19.  
  20. char *pgmName = "(bad pgm name)";
  21.  
  22. bool verboseArg = FALSE;
  23. char *swapArg = DEFAULT_SWAP_DIR;
  24. int nswapArg = 1;
  25. int nthreadsArg = 80;
  26.  
  27. void
  28. Fail(fmt, x, y)
  29.     char *fmt;
  30.     int x, y;
  31. {
  32.     if( fmt != NIL ) {
  33.         printf("ERROR: ");
  34.         printf(fmt, x, y);
  35.         printf("\n");
  36.     }
  37.     printf("Usage: %s [-v] [-swap dirname] [-nswap n] [-nthreads n]\n",
  38.             pgmName);
  39.     fflush(stdout);
  40.     exit(1);
  41. }
  42.  
  43.  
  44. void
  45. ParseArgs(argc, argv)
  46.     int argc;
  47.     char **argv;
  48. {
  49.     int argno;
  50.     char *arg;
  51.  
  52.     pgmName = argv[0];
  53.     argno = 1;
  54.     while( argno < argc ) {
  55.         arg = argv[argno];
  56.         if( strcmp(arg,"-v") == 0 ) {
  57.             verboseArg = TRUE;
  58.             argno++;
  59.             continue;
  60.         }
  61.         if( strcmp(arg, "-swap") == 0 ) {
  62.             if( (++argno) >= argc ) Fail("missing swap dir name", 0, 0);
  63.             swapArg = argv[argno];
  64.             argno++;
  65.             continue;
  66.         }
  67.         if( strcmp(arg, "-nswap") == 0 ) {
  68.             if( (++argno) >= argc ) Fail("missing swap dir count", 0, 0);
  69.             nswapArg = atoi(argv[argno]);
  70.             if( nswapArg <= 0 ) Fail("bad swap dir count %s", argv[argno], 0);
  71.             argno++;
  72.             continue;
  73.         }
  74.         if( strcmp(arg, "-nthreads") == 0 ) {
  75.             if( (++argno) >= argc ) Fail("missing threads count", 0, 0);
  76.             nthreadsArg = atoi(argv[argno]);
  77.             if( nthreadsArg <= 0 ) Fail("bad threads count %s", argv[argno], 0);
  78.             argno++;
  79.             continue;
  80.         }
  81.         if( (strcmp(arg, "-usage") == 0)
  82.                 || (strcmp(arg, "-help") == 0) 
  83.                 || (strcmp(arg, "-?") == 0) ) {
  84.             Fail(NIL, 0, 0);
  85.         }
  86.         if( arg[0] == '-' ) {
  87.             Fail("bad flag: %s", arg, 0);
  88.         }
  89.         Fail("bad arg: %s", arg, 0);
  90.     }
  91. }
  92.  
  93.  
  94. char *
  95. EnsureDir(name)
  96. {
  97.     int ans;
  98.     struct stat statBuf;
  99.  
  100.     if( verboseArg ) {
  101.         printf("Creating directory %s ... ", name);  fflush(stdout);
  102.     }
  103.     ans = stat(name, &statBuf);
  104.     if( ans >= 0 ) {
  105.         if( !S_ISDIR(statBuf.st_mode))
  106.             return "not a directory";
  107.         if( (statBuf.st_mode & 0777) != 0777 ) {
  108.             ans = chmod(name, 0777);
  109.             if( ans < 0 )
  110.                 return "no permission in directory";
  111.         }
  112.     } else {
  113.         ans = mkdir(name, 0777);
  114.         if( ans < 0 )
  115.             return "cannot create directory";
  116.     }
  117.     if( verboseArg ) {
  118.         printf("ok\n");  fflush(stdout);
  119.     }
  120.     return NIL;
  121. }
  122.  
  123. char *
  124. EnsureStickyFile(shortName, index)
  125.     char *shortName;
  126.     int index;
  127. {
  128.     char *msg;
  129.     int ans;
  130.     int fd;
  131.     char fnBuf[FILE_NAME_BUF_SIZE];
  132.  
  133.     ans = XR_SharedMemBSDMkFileName(fnBuf, (sizeof fnBuf), shortName, index);
  134.     if( ans < 0 ) return "bad file name (too long?)";
  135.     if( verboseArg ) {
  136.         printf("Creating file %s ... ", fnBuf); fflush(stdout);
  137.     }
  138.     ans = XR_SharedMemBSDGetBackingFile(shortName, index, 0, &fd);
  139.     if( ans < 0 ) return "can't create file";
  140.     (void) close(fd);
  141. #   if FAIL_IF_NOT_ISVTX
  142.         if( ans > 0 ) return "can't make file sticky";
  143. #   else
  144.         if( (ans > 0) && verboseArg ) {
  145.             printf("(can't make file sticky) ... ");
  146.         }
  147. #   endif
  148.  
  149.     if( verboseArg ) {
  150.         printf("ok\n");  fflush(stdout);
  151.     }
  152.     return NIL;
  153. }
  154.  
  155. main(argc, argv)
  156.     int argc;
  157.     char **argv;
  158. {
  159.     int ans, i, j;
  160.     char *msg;
  161.     char fnBuf[FILE_NAME_BUF_SIZE];
  162.  
  163.     ParseArgs(argc, argv);
  164.  
  165.     umask(0);    /* so file create modes work */
  166.  
  167.     ans = XR_SharedMemBSDSetSwapDir(swapArg, DI_NONE);
  168.     if( ans != 0 ) Fail("Bad swapdir name", 0, 0);
  169.  
  170.     msg = EnsureDir(swapArg);
  171.     if( msg != NIL ) Fail("%s: %s", swapArg, msg);
  172.     
  173.     for( i = 0; i < nswapArg; i++ ) {
  174.  
  175.         (void)XR_SharedMemBSDSetSwapDir(NIL, i);
  176.         ans = XR_SharedMemBSDMkFileName(fnBuf, (sizeof fnBuf), NIL, FI_NONE);
  177.         if( ans < 0 ) Fail("Bad swap subdir name %d", i, 0);
  178.         msg = EnsureDir(fnBuf);
  179.         if( msg != NIL ) Fail("%s: %s", fnBuf, msg);
  180.  
  181.         msg = EnsureStickyFile(FN_DATA, FI_NONE);
  182.         if( msg != NIL ) Fail("%s: %s", FN_DATA, msg);
  183.  
  184.         msg = EnsureStickyFile(FN_HEAP, FI_NONE);
  185.         if( msg != NIL ) Fail("%s: %s", FN_HEAP, msg);
  186.  
  187.         for( j = 0; j < nthreadsArg; j++ ) {
  188.             msg = EnsureStickyFile(FN_STACK, j);
  189.             if( msg != NIL ) Fail("%s: %s", fnBuf, msg);
  190.         }
  191.     }
  192.  
  193.     exit(0);
  194. }
  195.